#include<iostream>
#include<memory>
#include<vector>
#include<unordered_map>
//#include<cstdin>
#include<functional>
#include<unistd.h>
using namespace std;

// int main()
// {
//     int*a=new int(1);
//    std::weak_ptr<int> sp1(a);
//     cout<<"sp1.count: "<< sp1.use_count()<<endl;
//      std::weak_ptr<int> sp2(a);
//     cout<<"sp2.count: "<< sp2.use_count()<<endl;
//     //两个shareptr指向对同一个对象那么对应计数器count不会++
//     std::weak_ptr<int> sp3(sp1);
//     cout<<"sp3.count: "<< sp3.use_count()<<endl;
//     //sp3指向sp1则对应计数器count++
//     return 0;
// }

using TaskFunc=std::function<void()>;
using ReleaseFunc=std::function<void()>;

class TimeTask{
    private:
    uint64_t _id;
    uint32_t _timeout;
    TaskFunc _task_cb;
    ReleaseFunc _retask;
    public:
    TimeTask( uint64_t id,uint32_t timeout, const TaskFunc &cb):_id(id),_timeout(timeout),_task_cb(cb){}
    //构造
    ~TimeTask(){_task_cb();_retask();}//析构
    void SetRelease(const  ReleaseFunc&cb ){_retask=cb;}//释放
    uint32_t getdelaytime()
    {
        return _timeout;
    }
};

class TimerWheel
{
    private:
    int _tick;
    int _capacity;
    using weaktask=std::weak_ptr<TimeTask>;
    using PtrTask=std::shared_ptr<TimeTask>;
    vector<vector<PtrTask>> _wheel;
    unordered_map<uint64_t,weaktask> _timers;
 void RemoveTimer(uint64_t id)
    {
        auto it=_timers.find(id);
        if(it!=_timers.end())
        {
            _timers.erase(id);
        }
    }
   
    public:
    
    TimerWheel():_tick(0),_capacity(60){
        _wheel.resize(_capacity);//初始化
    }
    void Timeadd(uint64_t id,uint32_t delay,const TaskFunc& cb){
        PtrTask pt(new TimeTask(id,delay,cb));//new个sharedptr
        pt->SetRelease(bind( &TimerWheel::RemoveTimer,this,id));//TimeTask的SetRelease函数绑定成RemoveTimer函数
        int pos=(_tick+delay)%_capacity;
        _wheel[pos].push_back(pt);//sharedptr添加进轮子
        _timers[id]=weaktask(pt);//timers里面放的是weakptr不会增加count供后面刷新使用
    }//添加定时任务
    void TimeaRefrash(uint64_t id)//刷新
    {
        auto it=_timers.find(id);
        if(it==_timers.end())
        {
            return ;
        }
        PtrTask pt=it->second.lock();//用_timers的second对应weakptr构造sharedptr，不增加count
        //放进轮子
        int delay=pt->getdelaytime();
        int pos=(_tick+delay)%_capacity;
         _wheel[pos].push_back(pt);//添加进轮子
    }
    void RunTimerTask()
    {
        _tick=(_tick+1)%_capacity;
        _wheel[_tick].clear();//tick数组那个位置，对应位置的sharedptr就要释放
    }
};

class test
{
public:
test()
{
    cout<<"构造"<<endl;
}
~test()
{
    cout<<"析构"<<endl; 
}

};

void deltest(test* t)
{
    delete t;
}

int main()
{
    TimerWheel tw;
    test *t=new test();
   tw.Timeadd(123,3,bind(deltest,t));//add第三个参数传的是仿函数，deltest是轮里要的函数，t对象是deltest的参数
    
    for(int i=0;i<5;i++)
    {
        sleep(1);
        tw.RunTimerTask();
        tw.TimeaRefrash(123);
        cout<<"刷新----------"<<endl;
    }

    while(1)
    {
        sleep(1);
        tw.RunTimerTask();
        cout<<"---------"<<endl;
    }
    return 0;
}